/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.document.read.pdf;

import org.apache.pdfbox.pdmodel.PDPage;
import org.dopdf.document.model.Collection;
import org.dopdf.document.model.ExpressionEvaluator;
import org.dopdf.document.model.ResourceDetail;
import org.dopdf.utils.Constants;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * The collection class that represents the collection of pd pages..
 *
 * @author Deep Shah
 */
public class PDFPages {
    /** The collection of PDFPages represented by this collection. */
    private final Collection<PDFPage> pages;

    /**
     * The constructor that takes in a list of PDPage and converts it into the list of PDFPage.
     * @param allPages the reference of all pages held by this pdf document.
     */
    public PDFPages(final List<PDPage> allPages) {
        pages = new Collection<PDFPage>(new ArrayList<PDFPage>());
        new Collection<PDPage>(allPages).doInLoop(new ExpressionEvaluator<PDPage>() {

            /**
             * {@inheritDoc}
             */
            public void evaluate(final PDPage entity) {
                pages.add(new PDFPage(entity));
            }
        });
    }

    /**
     * The size of the pages collection.
     * @return the int value representing the total number of pages.
     */
    public int size() {
        return pages.size();
    }

    /**
     * Returns the image information of the desired pdf page.
     * @param pageNumber the page number whose image information is to be returned.
     * @return the ResourceDetail that represents the image information of the given pdf page.
     */
    public ResourceDetail getPageAsImage(final int pageNumber, final Constants.PageSize size) {
        return pages.get(pageNumber).asImage(size);
    }
}
